package com.gimi.test_jpa.com.gimi.controller.wqy;

import com.gimi.test_jpa.com.gimi.annotation.ResultInterceptor;
import com.gimi.test_jpa.com.gimi.mq.RocketMQProducer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.MessageSelector;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Api(description = "mq测试")
@RequestMapping("/test/mq")
@Slf4j
@ResultInterceptor
public class MqTestController {
    @Autowired
    private RocketMQProducer rocketMQProducer;

    @PostMapping("/sendMsg")
    @ApiOperation("mq发送普通消息测试")
    public void test(String msg){
        rocketMQProducer.sendMsg("queue_test_topic",msg);
    }

    @PostMapping("/sendTagMsg")
    @ApiOperation("mq发送tag消息测试")
    public void testTag(String msg){
        rocketMQProducer.sendTagMsg("queue_test_topic","tag_test",msg);
    }

    @PostMapping("/sendOrderly")
    @ApiOperation("mq发送顺序消息测试")
    public void sendOrderly(String msg){
        for (int i = 0; i <10 ; i++) {
            rocketMQProducer.sendOrderly("queue_test_topic:tag"+i,msg+i,"xixi");
        }
    }

    @PostMapping("/sendKeyMsg")
    @ApiOperation("mq发送普通key消息测试")
    public void sendKeyMsg() throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer("test_jpa_producer1");
        producer.setNamesrvAddr("47.98.100.116:9876");
        producer.start();
        for (int i = 0; i <10 ; i++) {
            Message message = new Message("queue_test_topic", "tag"+i, "key"+i, "testmsg".getBytes());
            SendResult send = producer.send(message);
            System.out.println(send);
        }
        producer.shutdown();
    }

    @PostMapping("/sendKeyOrderMsg")
    @ApiOperation("mq发送顺序key消息测试")
    public void sendKeyOrderMsg() throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer("test_jpa_producer1");
        producer.setNamesrvAddr("47.98.100.116:9876");
        producer.start();
        for (int i = 0; i <10 ; i++) {
            Message message = new Message("queue_test_topic", "tag"+i, "key"+i, "testmsg".getBytes());
            SendResult send = producer.send(message, new MessageQueueSelector() {
                @Override
                public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
                    Integer order=(Integer) arg;
                    return mqs.get(order);
                }
            },1);
            System.out.println(send);
        }
        producer.shutdown();
    }
}
